<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */
/**
 *	MEMCACHE支持，需事先启用PHP的MEMCACHE EXTENSION
 *	doc: http://cn2.php.net/memcached.getmulti
 *	Memcached::getMulti
 *	PECL memcached >= 0.1.0
 *
 *	Need Define:
 *		CONFIG_MEMCACHE_HOST
 *		CONFIG_MEMCACHE_PORT
 *		CONFIG_MEMCACHE_MULTI
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/

class cache_memcache implements cache_interface {

	public $memcache;
	
	public function __construct() {
	
	}
	
	//private $memcache;
	private $support_getmulti;

	public function __get($var) {
		if($var == 'memcache') {
			if(extension_loaded('Memcached')) {
				$this->memcache = new Memcached;
			} elseif(extension_loaded('Memcache')) {
				$this->memcache = new Memcache;
			} else {
				new Log('error #2. Memcache Extension not loaded.');
				exit();
			}
			if(!$this->memcache) {
				new Log('error #2. PHP.ini Error: Memcache extension not loaded.');
				exit();
			}
	 		if($this->memcache->connect(CONFIG_MEMCACHE_HOST, CONFIG_MEMCACHE_PORT)) {
	 			$this->support_getmulti = !empty(CONFIG_MEMCACHE_MULTI) || method_exists($this->memcache, 'getMulti');
	 			return $this->memcache;
	 		} else {
				new Log('error #2. Can not connect to Memcached host.');
				exit();
	 		}
		}
	}

	public function get($key) {
		$data = array(); 
		if(is_array($key)) {
			// 安装的时候要判断 Memcached 版本！ getMulti()
			if($this->support_getmulti) {
				$arrlist = $this->memcache->getMulti($key);
				// 会丢失 key!，补上 key
				foreach($key as $k) {
					!isset($arrlist[$k]) && $arrlist[$k] = FALSE;
				}
				return $arrlist;
			} else {
				foreach($key as $k) {
					$arr = $this->memcache->get($k);
					$data[$k] = $arr;
				}
				return $data;
			}
		} else {
			$data = $this->memcache->get($key);
			return $data;
		}
	}

	public function set($key, $value, $life = 0) {
		return $this->memcache->set($key, $value, 0, $life);
	}

	public function update($key, $value) {
		$arr = $this->get($key);
		if($arr !== FALSE) {
			is_array($arr) && is_array($value) && $arr = array_merge($arr, $value);
			return $this->set($key, $arr);
		}
		return 0;
	}

	public function delete($key) {
		return $this->memcache->delete($key);
	}
	
	public function truncate($pre = '') {
		return $this->memcache->flush();
	}
	
	public function maxid($table, $val = FALSE) {
		$key = $table.'-Auto_increment';
		if($val === FALSE) {
			return intval($this->get($key));
		} elseif(is_string($val) && $val{0} == '+') {
			$val = intval($val);
			$val += intval($this->get($key));
			$this->set($key, $val);
			return $val;
		} else {
			 $this->set($key, $val);
			 return $val;
		}
	}
	
	public function count($table, $val = FALSE) {
		$key = $table.'-Rows';
		if($val === FALSE) {
			return intval($this->get($key));
		} elseif(is_string($val)) {
			if($val{0} == '+') {
				$val = intval($val);
				$n = intval($this->get($key)) + $val;
				$this->set($key, $n);
				return $n;
			} else {
				$val = abs(intval($val));
				$n = max(0, intval($this->get($key)) - $val);
				$this->set($key, $n);
				return $n;
			}
		} else {
			$this->set($key, $val);
			return $val;
		}
	}
	
}

/*{TranslateTagEnd}*/

?>